#region File Header
//-----------------------------------------------------------------------------
// Bricks' Bane
//
// Copyright (C) 2010 Julien Villers
// This program is distributed under the terms of the 
// GNU Lesser General Public License (LGPL).
// See Docs/lgpl.txt and Docs/gpl.txt
//
// This file is based on GameStateManagement sample (C) Microsoft.
//-----------------------------------------------------------------------------
#endregion

using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework.Content;
using SibLib.GameState;
using SibLib.Sound;

namespace BricksBane.Screens
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    sealed class OptionsMenuScreen : MenuScreen
    {
        #region Fields

        private ContentManager m_Content;
        private List<string> m_Credits;

        MenuEntryNumber m_EntrySoundVolume;
        MenuEntryNumber m_EntryMusicVolume;
        MenuEntryToggle m_EntryVibration;
        MenuEntry m_EntryHighscores;
        MenuEntry m_EntryCredits;

        #endregion

        #region Initialization

        /// <summary>
        /// Constructor.
        /// </summary>
        public OptionsMenuScreen()
            : base("Options")
        {
            // Create our menu entries.
            m_EntrySoundVolume = new MenuEntryNumber("Sound: ", 10);
            m_EntryMusicVolume = new MenuEntryNumber("Music: ", 10);
            m_EntryVibration = new MenuEntryToggle("Vibration: ");
            m_EntryHighscores = new MenuEntry("Highscores");
            m_EntryCredits = new MenuEntry("Credits");

            MenuEntry backMenuEntry = new MenuEntry("Back");

            // Hook up menu event handlers.
            m_EntrySoundVolume.ValueChanged += new System.EventHandler<PlayerIndexEventArgs>(MenuEntrySoundVolumeChanged);
            m_EntryMusicVolume.ValueChanged += new System.EventHandler<PlayerIndexEventArgs>(MenuEntryMusicVolumeChanged);
            m_EntryVibration.Selected += new System.EventHandler<PlayerIndexEventArgs>(MenuEntryVibrationSelected);
            m_EntryHighscores.Selected += new System.EventHandler<PlayerIndexEventArgs>(MenuEntryHighscoresSelected);
            m_EntryCredits.Selected += new System.EventHandler<PlayerIndexEventArgs>(MenuEntryCreditsSelected);

            backMenuEntry.Selected += OnCancel;
            
            // Add entries to the menu.
            MenuEntries.Add(m_EntrySoundVolume);
            MenuEntries.Add(m_EntryMusicVolume);
            MenuEntries.Add(m_EntryVibration);
            MenuEntries.Add(m_EntryHighscores);
            MenuEntries.Add(m_EntryCredits);
            MenuEntries.Add(backMenuEntry);
        }

		public override void LoadContent()
        {
			base.LoadContent();

            m_Content = new ContentManager(ScreenManager.Game.Services, "Content");

            m_Credits = new List<string>(256);
            using (StreamReader reader = new StreamReader(m_Content.RootDirectory + "/credits.txt"))
            {
                while (!reader.EndOfStream)
                    m_Credits.Add(reader.ReadLine());
            }
            
            m_SoundManager = (ISoundManager)ScreenManager.Game.Services.GetService(typeof(ISoundManager));

            m_EntryMusicVolume.Value = (int)System.Math.Round(m_SoundManager.GetVolume("Music") * 10.0f);
            m_EntrySoundVolume.Value = (int)System.Math.Round(m_SoundManager.GetVolume("Default") * 10.0f);
            m_EntryVibration.Value = InputState.IsVibrationEnabled;
        }

		public override void UnloadContent()
        {
            base.UnloadContent();

            m_Content.Unload();

            m_Credits.Clear();
        }

        #endregion

        #region Handle Input

        void MenuEntrySoundVolumeChanged(object sender, PlayerIndexEventArgs e)
        {
            float value = m_EntrySoundVolume.Value / 10.0f;
            m_SoundManager.SetVolume("Default", value);
            m_SoundManager.SetVolume("SFX", value);
        }

        void MenuEntryMusicVolumeChanged(object sender, PlayerIndexEventArgs e)
        {
            float value = m_EntryMusicVolume.Value / 10.0f;
            m_SoundManager.SetVolume("Music", value);
        }

        void MenuEntryVibrationSelected(object sender, PlayerIndexEventArgs e)
        {
            InputState.IsVibrationEnabled = m_EntryVibration.Value;
        }

        void MenuEntryHighscoresSelected(object sender, PlayerIndexEventArgs e)
        {
            HighscoreScreen highscoreScreen = new HighscoreScreen();

            CopyCueNamesTo(highscoreScreen);

            ScreenManager.AddScreen(highscoreScreen, ControllingPlayer);
        }

        void MenuEntryCreditsSelected(object sender, PlayerIndexEventArgs e)
        {
            ScrollingTextScreen creditsScreen = new ScrollingTextScreen();

            creditsScreen.Texts.AddRange(m_Credits);
            creditsScreen.ScrollingSpeed = 2.0f;
            ScreenManager.AddScreen(creditsScreen, e.PlayerIndex);
        }

        #endregion
    }
}
